﻿var cartServers=new Object();
var jsp_bus=0;
function AddCartServer(client,args){
	if(client!=null){
		client.app=this;
		client.report=reportServerStatus;
		cartServers[client.referrer]={
			online:true,
			loading:0,
			uri:"rtmp://"+client.ip+"/"+args,
			client:client
			};
	}
}
function DelCartServer(client){
	if(client!=null){
		cartServers[client.referrer].online=false;
	}
}

function reportServerStatus(info){
	this.app.cartServers[this.referrer].loading=Number(info.loading);
}

function GetLessLoadingCartServer(){
	var LessServer;
	var count=0;
	for(var i in cartServers){
		count++;
		if(cartServers[i].online){
			var ser=cartServers[i]
			if(LessServer==undefined){
				LessServer=ser;
			}
			else if(LessServer.loading>ser.loading){
				LessServer=ser;
			}
		}
	}

	if(count<Number(configs.maxChatServerCount)){
		jsp_bus++;
		return {
			online:false,
			loading:0,
			uri:configs.chatServerUri+"backupCS"+jsp_bus.toString(),
			client:null
			}
	}
	return LessServer
}